package mo.tags.stack;

import java.util.Stack;

public class L856 {

    public int scoreOfParentheses(String s) {
        Stack<Integer> stack = new Stack<>();
        stack.push(0);
        for (char c : s.toCharArray()) {
            if (c == '(') {
                stack.push(0);
            } else {
                Integer top = stack.pop();
                int cur = stack.pop() + Math.max(2*top, 1);
                stack.push(cur);
            }
        }
        return stack.pop();
    }

}
